<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class report_data_model extends MY_Model {

	const TABLE = 'report_data';

	public function add($item_id, $data_value, $data_date, $data_server) {
		$data = array(
			'item_id'      => $item_id,
			'data_value'   => $data_value,
			'update_time'  => time(),
			'data_server'  => $data_server,
			'data_date' => $data_date
		);
		$this->db->insert(self::TABLE, $data);
		return $this->db->insert_id();
	}

	public function update($data_id, $data_value, $data_date) {
		$data = array(
			'data_value'  => $data_value,
			'update_time' => time(),
			'data_date'   => $data_date,
		);
		$this->db->where('data_id', $data_id);
		$this->db->update(self::TABLE, $data);
		return $this->db->insert_id();
	}

	public function get($data_value) {
		$query = $this->db->get_where(self::TABLE,array('data_value' => $data_value));
		return $query->row_array();
	}

	public function get_list_by_time($date) {
		$query = $this->db->get_where(self::TABLE, array('data_date' => $date));
		return $query->result_array();
	}

	public function get_by_timekey($data_date, $item_id) {
		$this->db->where_in('data_date', $data_date);
		$query = $this->db->get_where(self::TABLE, array('item_id' => $item_id));
		return $query->row_array();
	}
	
	public function get_by_id_and_date($item_id, $date) {
		$query = $this->db->get_where(self::TABLE, array('item_id' => $item_id, 'data_date' => $date));
		return $query->row_array();
	}

	public function get_data_list() {
		$query = $this->db->query("select * from report_data order by data_id desc");
		return $query->result_array();
	}

	public function last_month_data($item_id) { 
		$this->db->limit(30);
		$this->db->order_by('data_id', "desc");
		$query = $this->db->get_where(self::TABLE, array('item_id' => $item_id));
		return $query->result_array();
	}

}